package com.healthcarex.hip.saas.message.hl7.v3.namespace;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

import javax.xml.XMLConstants;

public class MapNamespaceContext extends AbstractNamespaceContext {
    private Map<String, String> pairs;
    private static final String resourceName = "XMLNamespace.properties";

    public MapNamespaceContext() {
        pairs = new HashMap<>();
        pairs.put(XMLConstants.XML_NS_PREFIX, XMLConstants.XML_NS_URI);
        pairs.put("", "urn:hl7-org:v3");
        pairs.put("s", "urn:hl7-org:v3");
        pairs.put("urn", "urn:hl7-org:v3");

        InputStream in = null;

        try {
            Properties properties = new Properties();
            in = this.getClass().getClassLoader()
                    .getResourceAsStream(resourceName);
            properties.load(in);

            for (Entry<Object, Object> prop : properties.entrySet()) {
                pairs.put((String) prop.getKey(), (String) prop.getValue());
            }
        } catch (Exception e) {
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public String getNamespaceURI(String prefix) {
        return pairs.get(prefix);
    }

    @Override
    public String getPrefix(String namespaceURI) {
        throw new UnsupportedOperationException("getPrefix");
    }

    @Override
    public Iterator getPrefixes(String namespaceURI) {
        throw new UnsupportedOperationException("getPrefixs");
    }

}
